/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pruebaFruela.java
 *
 * Created on 29-abr-2011, 13:46:05
 */

package interfaces;

import Controlador.Controlador;
import GestorCitas.LineaReceta;
import GestorFarmacia.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import paciente.Interfaces;
import paciente.PacienteView;

import paciente.Interfaz;

/**
 *
 * @author frpp
 */
public class verEntregas extends javax.swing.JPanel {
    private PacienteView app;
    private Entrega[] _entregas = null;
    private Interfaces _anterior;
    /** Creates new customizer NewCustomizer */
    public verEntregas(PacienteView a) {
        app = a;
        initComponents();

        panel_entrega.setRowHeight(32);
        panel_entrega.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }


    public void rellenaDatos(Integer id_paciente, Interfaces ant) throws SQLException, IOException, ClassNotFoundException, ParseException {
        _anterior = ant;
        _entregas = Controlador.obtenerEntregas(id_paciente);
        DateFormat fecha = new  SimpleDateFormat("dd/MM/yyyy");
        Interfaz.limpiarTabla(panel_entrega);
        if (_entregas == null) { // No hay entregas => no se rellena la tabla
            app.abrirMensajeError("El paciente no tiene ninguna entrega pendiente.", _anterior);
            return;
        }
        for(Entrega e:_entregas) {
            LineaReceta l = LineaReceta.get(e.getIdLineaReceta());
            Medicamento m = Medicamento.get(l.getIdMedicamento());
            String [] valores = {""+m.getNombreMedicamento(),""+m.getDCI(),""+ l.getNumTomas(),""+m.getDosisPorUnidad(),""+fecha.format(e.getFechaEntrega())};
            ((DefaultTableModel)panel_entrega.getModel()).addRow(valores);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        app.volver(_anterior);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        panel_entrega = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(780, 470));
        setMinimumSize(new java.awt.Dimension(780, 470));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Entrega", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 0, 18))); // NOI18N
        jPanel1.setMaximumSize(new java.awt.Dimension(780, 470));
        jPanel1.setName(""); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(746, 300));

        jScrollPane2.setMaximumSize(new java.awt.Dimension(2147483647, 0));
        jScrollPane2.setMinimumSize(new java.awt.Dimension(45, 0));
        jScrollPane2.setName("jScrollPane2"); // NOI18N

        panel_entrega.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        panel_entrega.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Medicamento", "Marca", "Cantidad recetada", "Cantidad por caja", "Fecha entrega"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        panel_entrega.setName("panel_entrega"); // NOI18N
        jScrollPane2.setViewportView(panel_entrega);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(paciente.PacienteApp.class).getContext().getActionMap(verEntregas.class, this);
        jButton2.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(paciente.PacienteApp.class).getContext().getResourceMap(verEntregas.class);
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setLabel(resourceMap.getString("jButton2.label")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleName("");
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable panel_entrega;
    // End of variables declaration//GEN-END:variables

    private javax.swing.JTable tabla;
}
